package com.richie696.rabbit.order.listener;

import com.rabbitmq.client.Channel;
import com.richie696.rabbit.commons.RabbitKey;
import com.richie696.rabbit.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderExpiredListener {

    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = RabbitKey.ORDER_QUEUE_DLX, ackMode = "MANUAL")
    public void onMessage(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
                          @Header(name = "userId") String userId, @Payload String message) {
        orderService.doExpiredRefresh(Long.valueOf(userId), message);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
